<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use EloquentFilter\Filterable;
use App\Models\Traits\Admin\ActionButtonTrait;

class FreightWayDemo extends Model
{
    use Filterable, ActionButtonTrait;

    protected $table = 'freight_way_demos';

    protected $append = ['actionButton'];
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    
    protected $actions = ['edit', 'config','delete'];


    protected $actionUri = 'freightWayDemos';

    protected $fillable = [
        'name', 'freight_num'
    ];

    public function mainborad()
    {
    	return $this->belongsTo(Mainborad::class, 'mainborad_id', 'id');
    }

    protected static function boot()
    {
        parent::boot();
        static::created(function($model){
        	$num = $model->freight_num;
        	for ($i=0; $i < $num; $i++) { 
        		$data[$i] = new FreightWayDemoData([
        			'capacity' => 0,
        			'state' => 0,
        			'type' => 1,
        			'No' => $i + 1,
        		]);
        	}
        	$model->freightWayDemoData()->saveMany($data);
        });
    }

    public function freightWayDemoData()
    {
    	return $this->hasMany(FreightWayDemoData::class, 'freight_way_demo_id', 'id');
    }
}
